package com.yc.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yc.utils.StringUtil;

/**
 *列车数据库操作
 * @author UNO
 * 2021年6月30日下午4:59:43
 */
public class TrainDao {
	/**
	 * 查询列车表所有所有列车的基本信息
	 * @return
	 */
	public List<Map<String, String>> findAllTrain(){
		DBHelper db = new DBHelper();
		String sql = "select * from tb_train where c_status = 0 ";
		return db.finds(sql);
	}

	/**
	 * 多条件查询
	 * @param startstation
	 * @param destination
	 * @return
	 */
	public List<Map<String, String>> findUsefulTrain(String startstation, String destination){
		DBHelper db = new DBHelper();
		String sql = "select * from tb_train where c_status = 0 ";
		List<Object> params = new ArrayList<Object>();
		if (!StringUtil.checkNull(startstation)) {
			sql += "and c_startstation = ? ";
			params.add(startstation);
		}
		if (!StringUtil.checkNull(destination)) {
			sql += "and c_destination = ? ";
			params.add(destination);
		}
		sql += " order by c_id ";
		return db.finds(sql, params);
	}

	/**
	 * 查询列车表指定一趟列车的基本信息
	 * @return  List集合里一条Map，只存了一趟列车信息的值
	 */
	public Map<String, Object> find( String cname){
		DBHelper db = new DBHelper();
		String sql = "select * from tb_train where c_status = 0 and c_name = ? ";
		return db.findSingle(sql, cname);
	}

	/**
	 * 查询指定列车的站点
	 * @param cname
	 * @return
	 */
	public List<Map<String, String>> findSite(String cname){
		DBHelper db = new DBHelper();
		String sql = "select * from tb_moment where c_name = ? ";
		return db.finds(sql, cname);
	}

	/**
	 * 查找指定列车名的票数和票价
	 * @param cname
	 * @return
	 */
	public Map<String, Object> findPrice(String cname, String date){		
		//select * from tb_price where c_name = 'G5116'
		DBHelper db = new DBHelper();
		String sql = "select * from tb_price where c_name = ? and p_date = ?";
		return db.findSingle(sql, cname, date);
	}
	/**
	 * 查找一条订单, 用于判断用户是否买了同一时间的同一趟列车
	 * @param cname
	 * @return
	 */
	public Map<String, Object> findAOrder(String uname, String cname, String starttime){				
		DBHelper db = new DBHelper();
		String sql = "select * from tb_userorder where u_name = ? and c_name = ? and o_starttime = ?";
		return db.findSingle(sql,uname, cname, starttime);
	}

	/**
	 * 生成订单
	 * @param uname 预订人
	 * @param yname  乘车人
	 * @param cname 	列车名
	 * @param starttime	出发时间 有日期
	 * @param endtime	到达时间 有日期
	 * @param seat	座位号
	 * @param seattype	座位类型
	 * @param price	票价
	 * @return
	 */
	public int GenerateOrder(String uname, String yname, String cname, String starttime, String endtime,String seat, String seattype, String price) {
		//insert into tb_userorder values(seq_userorder_id.nextval, '张三', 'G5116', '2021/7/28 08:00', '2021/7/28 16:00', '18-58','硬座', '75')
		DBHelper db = new DBHelper();
		String sql = "insert into tb_userorder values(seq_userorder_id.nextval, ?, ?, ?, ?, ?, ?, ?, ? )";
		return db.update(sql, uname, yname, cname, starttime, endtime, seat, seattype, price);
	}

	
	/**
	 * 创建一条当天的列车票务表，没人在那一天购票，说明不需要这趟车
	 * @param cname
	 * @param date
	 * @return
	 */
	public int GeneratePrice(String cname,String date) {
		//insert into tb_price values(seq_price_id.nextval, 'G5116','2021/07/02', 100, 175, 100, 167, 100, 75, 100, 75);
		DBHelper db = new DBHelper();
		String sql = "insert into tb_price values(seq_price_id.nextval, ?, ?, 100, 175, 100, 167, 100, 75, 100, 75)";
		return db.update(sql, cname, date);
	}

	
	/**
	 * 修改票数
	 * @param seattype
	 * @param i
	 * @param cname
	 * @return
	 */
	public int updateNumberYZ(int i, String cname, String date) {
		//update tb_price set p_yz = (p_yz + 1) where c_name = 'G5116';
		DBHelper db = new DBHelper();
		String sql = "update tb_price set p_yz = (p_yz + (?)) where c_name = ? and p_date = ? ";
		return db.update(sql, i, cname, date);
	}
	public int updateNumberRW(int i, String cname, String date) {
		//update tb_price set p_yz = (p_yz + 1) where c_name = 'G5116';
		DBHelper db = new DBHelper();
		String sql = "update tb_price set p_rw = (p_rw + (?)) where c_name = ? and p_date = ? ";
		return db.update(sql, i, cname, date);
	}
	public int updateNumberYW(int i, String cname, String date) {
		//update tb_price set p_yz = (p_yz + 1) where c_name = 'G5116';
		DBHelper db = new DBHelper();
		String sql = "update tb_price set p_yw = (p_yw + (?)) where c_name = ? and p_date = ? ";
		return db.update(sql, i, cname, date);
	}
	public int updateNumberWZ(int i, String cname, String date) {
		//update tb_price set p_yz = (p_yz + 1) where c_name = 'G5116';
		DBHelper db = new DBHelper();
		String sql = "update tb_price set p_wz = (p_wz + (?)) where c_name = ? and p_date = ? ";
		return db.update(sql, i, cname, date);
	}

}
